﻿using Basic.FormCollect;
using Basic.FormModel.Column;
using Basic.FormModel.Control;
using Basic.FormModel.DB;
using Basic.FormModel.Model;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.Column.Model;
using Basic.FormService.Interface;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;

namespace Basic.FormService.lmpl
{
    internal class TableColumnService : ITableColumnService
    {
        private readonly ITableColumnCollect _Column;
        private readonly IControlCollect _Control;
        public TableColumnService ( ITableColumnCollect column, IControlCollect control )
        {
            this._Control = control;
            this._Column = column;
        }
        public bool SetSort ( QueryColSet set )
        {
            ColumnSort[] sorts = this._Column.GetSorts(set.TableId, set.GroupId);
            int index = sorts.FindIndex(c => c.Id == set.Id);
            if ( index == -1 )
            {
                return false;
            }
            ColumnSort sour = sorts[index];
            ColumnSort to;
            if ( set.IsNext )
            {
                index += 1;
                if ( index == sorts.Length )
                {
                    return false;
                }
                to = sorts[index];
            }
            else
            {
                index -= 1;
                if ( index == -1 )
                {
                    return false;
                }
                to = sorts[index];
            }
            int sort = to.Sort;
            to.Sort = sour.Sort;
            sour.Sort = sort;
            this._Column.SetSort(sour, to);
            return true;
        }
        public long Add ( TableColumnAdd add )
        {
            ColumnAdd data = add.ConvertMap<TableColumnAdd, ColumnAdd>();
            ControlBase control = this._Control.Get<ControlBase>(data.ControlId);
            data.DataType = control.DataType;
            data.FuncType = control.FuncType;
            return this._Column.Add(data);
        }

        public long AddFull ( ColumnFullAdd datum )
        {
            ColFullAdd add = datum.ConvertMap<ColumnFullAdd, ColFullAdd>();
            ControlBase control = this._Control.Get<ControlBase>(datum.ControlId);
            add.DataType = control.DataType;
            add.FuncType = control.FuncType;
            if ( add.ControlSet != null )
            {
                if ( add.ControlSet.TryGetValue("isMultiple", out object val) && Convert.ToBoolean(val) )
                {
                    add.DataType = FormRecordDataType.Array;
                }
                else if ( add.ControlSet.TryGetValue("multiple", out val) && Convert.ToBoolean(val) )
                {
                    add.DataType = FormRecordDataType.Array;
                }
            }
            return this._Column.AddCol(add);
        }

        public void Delete ( long id )
        {
            DBTableColumn col = this._Column.Get(id);
            this._Column.Delete(col);
        }

        public TableColumnDto Get ( long id )
        {
            DBTableColumn col = this._Column.Get(id);
            return col.ConvertMap<DBTableColumn, TableColumnDto>();
        }
        public bool Set ( long id, TableColumnSet set )
        {
            DBTableColumn col = this._Column.Get(id);
            ColumnSet data = set.ConvertMap<TableColumnSet, ColumnSet>();
            set.EditControl ??= string.Empty;
            ControlBase control = this._Control.Get<ControlBase>(set.ControlId);
            data.DataType = control.DataType;
            data.FuncType = control.FuncType;
            if ( set.ControlSet != null )
            {
                if ( set.ControlSet.TryGetValue("isMultiple", out object val) && Convert.ToBoolean(val) )
                {
                    data.DataType = FormRecordDataType.Array;
                }
                else if ( set.ControlSet.TryGetValue("multiple", out val) && Convert.ToBoolean(val) )
                {
                    data.DataType = FormRecordDataType.Array;
                }
            }
            return this._Column.Set(col, data);
        }

        public bool SetColSpan ( long id, int span )
        {
            DBTableColumn col = this._Column.Get(id);
            return this._Column.SetColSpan(col, span);
        }
        public void SetColSpan ( KeyValuePair<long, int>[] span )
        {
            this._Column.SetColSpan(span);
        }
        public bool SetGroupId ( long id, long groupId )
        {
            DBTableColumn col = this._Column.Get(id);
            return this._Column.SetGroupId(col, groupId);
        }

        public void SetSort ( KeyValuePair<long, int>[] sort )
        {
            this._Column.SetSort(sort);
        }
    }
}
